package com.freesky.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.freesky.biz.activity.domain.TAlipayCouponDetail;
import com.freesky.biz.activity.domain.TCouponDetail;
import com.freesky.biz.activity.mapper.CouponDetailMapper;
import com.freesky.biz.pay.service.ICouponStockService;
import com.freesky.biz.rights.domain.ArrearsDetailEntity;
import com.freesky.biz.rights.domain.RightsCouponDetailEntity;
import com.freesky.biz.rights.domain.RightsPayIcbcEntity;
import com.freesky.biz.rights.mapper.ArrearsDetailMapper;
import com.freesky.biz.rights.mapper.RightsCouponDetailMapper;
import com.freesky.biz.rights.service.IArrearsDetailService;
import com.freesky.biz.rights.service.IRightsPayIcbcService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.constant.LotteryConstants;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.redis.RedisCache;
import com.freesky.common.utils.StringUtils;
import com.freesky.framework.web.service.SendAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import com.freesky.biz.pay.service.SinopecService;
/**
 * 批量补发券
 */
@Component("rightsArrearsTask")
public class RightsArrearsTask {

    private static final Logger logger = LoggerFactory.getLogger(RightsArrearsTask.class);

    @Autowired
    private SendAlarmService sendAlarmService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ICouponStockService couponStockService;

    @Autowired
    private SinopecService sinopecService;

    @Autowired
    private CouponDetailMapper couponDetailMapper;

    @Autowired
    private ArrearsDetailMapper arrearsDetailMapper;

    @Autowired
    private RightsCouponDetailMapper rightsCouponDetailMapper;

    public void taskProcess() {
        logger.info("rightsArrearsTask task start");

        List<ArrearsDetailEntity> arrearsDetailEntityList = arrearsDetailMapper.selectUnSendList();

        for(ArrearsDetailEntity arrearsDetailEntity : arrearsDetailEntityList) {
            arrearsDetailEntity.setSendFlag("3");
            arrearsDetailMapper.updateById(arrearsDetailEntity);
            //微信立减金
            if("1".equalsIgnoreCase(arrearsDetailEntity.getType())){
                TCouponDetail couponDetail = new TCouponDetail();
                couponDetail.setId(SeqUtil.getSeqId());
                couponDetail.setOpenId(arrearsDetailEntity.getOpenId());
                couponDetail.setStockId(arrearsDetailEntity.getStockId());
                couponDetail.setMchId(arrearsDetailEntity.getMchId());
                couponDetail.setBusiId(arrearsDetailEntity.getId());
                couponDetail.setActivityId(arrearsDetailEntity.getActivityId());
                couponDetail.setAmt(String.valueOf(arrearsDetailEntity.getAmt()));
                couponDetail.setAppId(arrearsDetailEntity.getAppId());
                couponDetail.setReissueFlag("1");
                couponDetail.setRemarks("补发");
                couponDetail.setBusiId(arrearsDetailEntity.getId());
                AjaxResult ajaxResult = couponStockService.grantCouponsOpenId(couponDetail);
                couponDetail.setStatusMsg(ajaxResult.getMsg());
                couponDetailMapper.addCouponDetail(couponDetail);
                if(ajaxResult.isSuccess()){
                    arrearsDetailEntity.setSendFlag("1");
                } else {
                    arrearsDetailEntity.setSendFlag("2");
                    arrearsDetailEntity.setSendResult(ajaxResult.getMsg());
                }
                arrearsDetailMapper.updateById(arrearsDetailEntity);
            } else if("2".equalsIgnoreCase(arrearsDetailEntity.getType())){
                //支付宝红包
                TAlipayCouponDetail couponDetail = new TAlipayCouponDetail();
                couponDetail.setId(SeqUtil.getSeqId());
                couponDetail.setAmt(String.valueOf(arrearsDetailEntity.getAmt()));
                couponDetail.setOpenId(arrearsDetailEntity.getMobile());
                couponDetail.setBusiId(arrearsDetailEntity.getId());
                couponDetail.setActivityId(arrearsDetailEntity.getActivityId());
                couponDetail.setStockId(arrearsDetailEntity.getStockId());
                couponDetail.setReissueFlag("1");
                couponDetail.setBusiId(arrearsDetailEntity.getId());
                AjaxResult ajaxResult = couponStockService.grantAlipayCoupon(couponDetail);
                couponDetailMapper.addAliCouponDetail(couponDetail);
                if(ajaxResult.isSuccess()){
                    arrearsDetailEntity.setSendFlag("1");
                } else {
                    arrearsDetailEntity.setSendFlag("2");
                    arrearsDetailEntity.setSendResult(ajaxResult.getMsg());
                }
                arrearsDetailMapper.updateById(arrearsDetailEntity);
            } else if("11".equalsIgnoreCase(arrearsDetailEntity.getType())){
                //中石化森美
                RightsCouponDetailEntity rightsCouponDetail = new RightsCouponDetailEntity();
                rightsCouponDetail.setId(SeqUtil.getSeqId());

                AjaxResult ajaxResult = sinopecService.sendCrmPackage(rightsCouponDetail.getId(),
                        arrearsDetailEntity.getStockId(),
                        arrearsDetailEntity.getMobile());

                if(ajaxResult.isSuccess()){
                    rightsCouponDetail.setCouponId(ajaxResult.get(AjaxResult.DATA_TAG).toString());
                    rightsCouponDetail.setSendStatus("1");

                    arrearsDetailEntity.setSendFlag("1");
                } else {
                    rightsCouponDetail.setSendStatus("2");

                    arrearsDetailEntity.setSendFlag("2");
                    arrearsDetailEntity.setSendResult(ajaxResult.getMsg());
                }
                rightsCouponDetail.setReason(ajaxResult.get(AjaxResult.MSG_TAG).toString());

                rightsCouponDetail.setReceiveId(arrearsDetailEntity.getId());
                rightsCouponDetail.setBusiId("");
                rightsCouponDetail.setBusiName("");
                rightsCouponDetail.setActivityRuleId(arrearsDetailEntity.getId());
                rightsCouponDetail.setActivityId(arrearsDetailEntity.getActivityId());
                rightsCouponDetail.setCouponType("11");
                rightsCouponDetail.setAppId("");
                rightsCouponDetail.setOpenId(arrearsDetailEntity.getMobile());
                rightsCouponDetail.setMobile(arrearsDetailEntity.getMobile());
                rightsCouponDetail.setStockId(arrearsDetailEntity.getStockId());
                rightsCouponDetail.setAmt(arrearsDetailEntity.getAmt().intValue());
                rightsCouponDetail.setCreateTime(new Date());
                rightsCouponDetail.setDelFlag("0");
                rightsCouponDetail.setEndTime(DateUtil.offsetDay(new Date(), 15));
                rightsCouponDetail.setReissueFlag("1");
                rightsCouponDetailMapper.insert(rightsCouponDetail);

                arrearsDetailMapper.updateById(arrearsDetailEntity);
            }
        }
        logger.info("rightsArrearsTask task end");
    }
}
